const formatTime = date => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()

  return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':')
}

const formatNumber = n => {
  n = n.toString()
  return n[1] ? n : '0' + n
}

const formatTimeYM = date => {
  let year = date.getFullYear()
  let month = date.getMonth() + 1

  year = formatNumber(year)
  month = formatNumber(month)
  return year + "年" + month+"月"
}



const getWindowHeight_rpx = () => {

  // px换算为rpx，因为屏幕长度为750rpx，w/750rpx = h/rpx

  let sysInfo = wx.getSystemInfoSync()
  let w = sysInfo.windowWidth
  let h = sysInfo.windowHeight
  let rpx = 750 / w * h

  return rpx
}

const showDialog = (title, content, confirm)=>{
  wx.showModal({
    title: title,
    content: content,
    cancelText: '确定',
    cancelColor: '#F24949',
    confirmText: '取消',
    confirmColor: '#000000',
    success: res => {
      if (res.confirm) {
        //这里是取消，因为调换了按钮的位置
      } else if (res.cancel) {
        //这里是确定
        confirm()
      }
    }
  })
}


const showDialogMsg = (msg, confirmText) => {
  wx.showModal({
    title: '',
    content: msg,
    showCancel: false,
    confirmText: confirmText || '知道了',
    confirmColor: '#2E8AE6',
  })
}

const billSortByMonth = (billList, newbillList)=>{
  // 主要是看start_data，先按月份分好类，然后月份再排序，格式如下，[{month:"2017年12月",paymoney:"",notpaymoney:"",billList:[{},{}]}]
  let monthBillList = []
  for (let i = billList.length-1; i >=0; i--){
    let bill = billList[i]
    // 取出月份值
    let start_date = new Date(bill.start_date)
    let month = formatTimeYM(start_date)   

    // 判断该month是否存在monthBillList中
    if (isMonthIn(month, monthBillList)){
      let monthIndex = getMonthIndex(month, monthBillList)
      monthBillList[monthIndex].billList.push(bill)
    }else{
      monthBillList.push({ month: month, billList: [bill]})
    }
  }

  // 统计金额 haspaymoney包括了SUCCESS和CLOSED，notpaymoney就只有NOTPAY
  for (let i = 0; i < monthBillList.length; i++) {
    let _billList = monthBillList[i].billList

    let haspaymoney = 0
    let notpaymoney = 0
    for (let j = 0; j < _billList.length;j++){
      let _bill = _billList[j]
      if (_bill.trade_state === "NOTPAY"){

        notpaymoney = notpaymoney + parseInt(_bill.total_fee)
      }else{

        if (_bill.time_end){
          _bill.time_end = _bill.time_end.substr(0, 4) + "/" + _bill.time_end.substr(4, 2) + "/" + _bill.time_end.substr(6, 2) + " " + _bill.time_end.substr(8, 2) + ":" + _bill.time_end.substr(10, 2) + ":" + _bill.time_end.substr(12, 2)
        }

        if (_bill.close_info) {
          _bill.close_info.moneyday = _bill.close_info.moneyday.substr(0, 4) + "/" + _bill.close_info.moneyday.substr(5, 2) + "/" + _bill.close_info.moneyday.substr(8, 2)
        }

        
        haspaymoney = haspaymoney + parseInt(_bill.total_fee)
      }
    }
    monthBillList[i].haspaymoney = haspaymoney
    monthBillList[i].notpaymoney = notpaymoney
  }

  return monthBillList
}

const isMonthIn = (month,monthBillList)=>{

  for (let i = 0; i < monthBillList.length; i++) {
    let monthBill = monthBillList[i]
    if (monthBill.month === month) {
      return true
    }
  }

  return false
}

const getMonthIndex = (month, monthBillList) => {

  for (let i = 0; i < monthBillList.length;i++){
    let monthBill = monthBillList[i]
    if (monthBill.month === month) {
      return i
    }
  }
  return -1
}






//------
const cashSortByMonth = (cashList, newcashList) => {
  // 主要是看start_data，先按月份分好类，然后月份再排序，格式如下，[{month:"2017年12月",paymoney:"",notpaymoney:"",billList:[{},{}]}]
  let monthCashList = []
  for (let i = cashList.length - 1; i >= 0; i--) {
    let cash = cashList[i]
    // 取出月份值
    let create_date = new Date(cash.create_date)
    let month = formatTimeYM(create_date)

    cash.create_date = formatTime(create_date)
    // 判断该month是否存在monthBillList中
    if (isMonthIn(month, monthCashList)) {
      let monthIndex = getMonthIndex(month, monthCashList)
      monthCashList[monthIndex].cashList.push(cash)
    } else {
      monthCashList.push({ month: month, cashList: [cash] })
    }
  }

  // 统计金额 haspaymoney包括了SUCCESS和CLOSED，notpaymoney就只有NOTPAY
  for (let i = 0; i < monthCashList.length; i++) {
    let _cashList = monthCashList[i].cashList

    let total_amount = 0
    for (let j = 0; j < _cashList.length; j++) {
      let _cash = _cashList[j]
      total_amount = total_amount + parseInt(_cash.amount)
    }
    monthCashList[i].total_amount = total_amount
  }

  return monthCashList
}



const getBookAddress = function () {
  return wx.getStorageSync('bookAddress') || ''
}

const saveBookAddress = function (bookAddress) {
  wx.setStorageSync('bookAddress', bookAddress)
}










module.exports = {
  formatTime: formatTime,
  showDialog: showDialog,
  showDialogMsg:showDialogMsg,
  billSortByMonth: billSortByMonth,
  cashSortByMonth: cashSortByMonth,
  getMonthIndex: getMonthIndex,
  formatTimeYM: formatTimeYM,
  getBookAddress: getBookAddress,
  saveBookAddress: saveBookAddress
}
